﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;

    public class RotateRuleDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected Button btnSaveContinue;
        protected CheckBox chbIsActive;
        protected DropDownList ddlLotAttributes;
        protected DropDownList ddlRotation;
        protected HtmlForm F2183;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnID;
        protected HtmlInputHidden hdnLotID;
        protected HtmlInputHidden hdnType;
        protected HtmlHead Head1;
        protected Label lblAreaDetail;
        protected Label lblDescr;
        protected Label lblLineID;
        protected Label lblLotAttributes;
        protected Label lblMsg;
        protected Label lblRotation;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDescr;
        protected TextBox txtLineID;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("RotateRule.aspx?ID=" + base.UrlEncode(this.hdnID.Value) + "&from=" + base.UrlEncode(this.hdnFrom.Value));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtLineID.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2420_006");
            }
            else
            {
                bool success = false;
                try
                {
                    RotationRuleDetailInfo rotationRuleDetail = new RotationRuleDetailInfo {
                        RotationRuleID = this.hdnID.Value,
                        LineID = Convert.ToInt32(this.txtLineID.Text.Trim()),
                        LotAttributes = this.ddlLotAttributes.SelectedValue,
                        Rotation = this.ddlRotation.SelectedValue,
                        Descr = this.txtDescr.Text.Trim(),
                        IsActive = this.chbIsActive.Checked ? "Y" : "N",
                        CreatedBy = base.UserInfo.UserID
                    };
                    if (!string.IsNullOrEmpty(this.hdnType.Value) && (this.hdnType.Value == "edit"))
                    {
                        rotationRuleDetail.UpdatedBy = base.UserInfo.UserID;
                        if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(rotationRuleDetail, "ROTATION_RULE_DETAIL")))
                        {
                            success = true;
                        }
                    }
                    else if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(rotationRuleDetail, "ROTATION_RULE_DETAIL")))
                    {
                        success = true;
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2420_007") + ex.Message;
                    return;
                }
                if (success)
                {
                    base.Response.Redirect("RotateRule.aspx?from=" + base.UrlEncode(this.hdnFrom.Value) + "&type=detail&ID=" + base.UrlEncode(this.hdnID.Value));
                }
            }
        }

        protected void btnSaveContinue_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtLineID.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2420_006");
            }
            else
            {
                bool success = false;
                try
                {
                    RotationRuleDetailInfo rotationRuleDetail = new RotationRuleDetailInfo {
                        RotationRuleID = this.hdnID.Value,
                        LineID = Convert.ToInt32(this.txtLineID.Text.Trim()),
                        LotAttributes = this.ddlLotAttributes.SelectedValue,
                        Rotation = this.ddlRotation.SelectedValue,
                        Descr = this.txtDescr.Text.Trim(),
                        IsActive = this.chbIsActive.Checked ? "Y" : "N",
                        CreatedBy = base.UserInfo.UserID
                    };
                    if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(rotationRuleDetail, "ROTATION_RULE_DETAIL")))
                    {
                        success = true;
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2420_007") + ex.Message;
                    return;
                }
                if (success)
                {
                    this.lblMsg.Text = this.txtLineID.Text + base.GetGlobalMsgResource("C004");
                    this.txtLineID.Text = (Convert.ToInt32(this.txtLineID.Text.Trim()) + 10).ToString();
                    if (this.ddlLotAttributes.Items.Count > 0)
                    {
                        this.ddlLotAttributes.SelectedValue = this.ddlLotAttributes.Items[0].Value;
                    }
                    if (this.ddlRotation.Items.Count > 0)
                    {
                        this.ddlRotation.SelectedValue = this.ddlRotation.Items[0].Value;
                    }
                    this.txtDescr.Text = "";
                    this.chbIsActive.Checked = true;
                }
            }
        }

        private void LoadData()
        {
            this.SetDropDownLitData();
            if (base.Request.QueryString["ID"] != null)
            {
                this.hdnID.Value = base.Request.QueryString["ID"];
            }
            if (base.Request.QueryString["SeqNo"] != null)
            {
                this.txtLineID.Text = base.Request.QueryString["SeqNo"];
            }
            if (base.Request.QueryString["lotID"] != null)
            {
                this.ddlLotAttributes.SelectedValue = base.Request.QueryString["lotID"].ToString();
                this.hdnLotID.Value = base.Request.QueryString["lotID"].ToString();
            }
            if (base.Request.QueryString["from"] != null)
            {
                this.hdnFrom.Value = base.Request.QueryString["from"];
            }
            if (base.Request.QueryString["type"] != null)
            {
                this.btnSaveContinue.Enabled = false;
                this.hdnType.Value = base.Request.QueryString["type"].ToString();
                this.SetRotateRuleDetail2Form();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetDropDownLitData()
        {
            this.ddlLotAttributes.DataSource = CommoneMethodServiceClientProxy.GetInstance().GetSysCodeDataSource("LotAttributes");
            this.ddlLotAttributes.DataTextField = "SysCodeText";
            this.ddlLotAttributes.DataValueField = "SysCodeValue";
            this.ddlLotAttributes.DataBind();
            this.ddlRotation.DataSource = CommoneMethodServiceClientProxy.GetInstance().GetSysCodeDataSource("Rotation");
            this.ddlRotation.DataTextField = "SysCodeText";
            this.ddlRotation.DataValueField = "SysCodeValue";
            this.ddlRotation.DataBind();
        }

        private void SetRotateRuleDetail2Form()
        {
            if ((this.hdnID.Value != "") && (this.hdnLotID.Value != ""))
            {
                RotationRuleDetailInfo rotationRuleDetail = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.hdnID.Value.ToString(), this.hdnLotID.Value, "", "ROTATION_RULE_DETAIL") as RotationRuleDetailInfo;
                if (rotationRuleDetail != null)
                {
                    this.ddlLotAttributes.SelectedValue = rotationRuleDetail.LotAttributes;
                    this.ddlLotAttributes.Enabled = false;
                    this.ddlRotation.SelectedValue = rotationRuleDetail.Rotation;
                    this.txtDescr.Text = rotationRuleDetail.Descr;
                    this.chbIsActive.Checked = rotationRuleDetail.IsActive == "Y";
                }
            }
        }
    }
}

